package cn.zwx.business.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zwx.base.enums.RetCode;
import cn.zwx.base.exception.BizException;
import cn.zwx.business.goods.mapper.GoodsMapper;
import cn.zwx.business.goods.service.GoodsService;
import cn.zwx.order.model.GoodsInfo;
import cn.zwx.order.req.DeductionRepertoryReq;
import cn.zwx.order.req.QueryGoodsInfoReq;
import cn.zwx.order.resp.QueryGoodsInfoResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @description: 商品服务逻辑处理
 * @projectName:rocket-mq-example
 * @see:cn.zwx.business.goods.service.impl
 * @author:zhangwenxue
 * @createTime:2020/10/15 14:09
 * @version:1.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deductionRepertory(DeductionRepertoryReq repertoryReq) {
        //TODO 这里需要做幂等处理
        int row = goodsMapper.deductionRepertory(repertoryReq);
        if (row!=1){
            throw new BizException(RetCode.FAIL,"扣减库存失败!!");
        }
    }

    @Override
    public QueryGoodsInfoResp queryGoodsInfo(QueryGoodsInfoReq req) {
        //查询商品信息
        GoodsInfo goodsInfo = goodsMapper.selectByPrimaryKey(req.getGoodsId());
        //返回参数封装
        QueryGoodsInfoResp resp = new QueryGoodsInfoResp();
        BeanUtil.copyProperties(goodsInfo,resp);
        return resp;
    }
}
